package com.glowdayz.glowmee.bc;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.DateTime;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.glowmee.cmd.AdminTableCMD;
import com.glowdayz.glowmee.dao.AdminTableDAO;
import com.glowdayz.glowmee.dao.BrandDAO;
import com.glowdayz.glowmee.dao.CommonCodeDAO;

public class UpdateEventBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public UpdateEventBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idAdminBoard = Integer.parseInt(request.getParameter("id"));
		
		try {
			AdminTableCMD cmd = AdminTableDAO.getInstance().getEvent(idAdminBoard);
			
			FileInfo image = cmd.getImage();
			if (image != null) {
				request.setAttribute("image", image);
			}
			request.setAttribute("eventType", CommonCodeDAO.getInstance().getCommonCodeList(null, "parentId=3"));
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, -1, "brandTitle", null));
			request.setAttribute("cmd", cmd);
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/updateEvent.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		
		FileInfo finfo = mbox.upload2(request);
		
		int idAdminBoard = mbox.getInt("id");
		String boardTitle = mbox.getValue("boardTitle");
		int brand = mbox.getInt("brand");
		String start_date = mbox.getValue("start_date");
		start_date = start_date.replace("-", "").replace("T", "").replace(":", "") + "00";
		String end_date = mbox.getValue("end_date");
		end_date = end_date.replace("-", "").replace("T", "").replace(":", "") + "00";
		String boardText = mbox.getValue("boardText");
		int eventType = mbox.getInt("eventType");
		String eventLinkCode = mbox.getValue("eventLinkCode");
		int isDisplay = mbox.getInt("isDisplay");
		
		try {
			AdminTableCMD cmd = AdminTableDAO.getInstance().getEvent(idAdminBoard);
			FileInfo image = cmd.getImage();
			if (image != null && finfo != null) {
				DBHandler.getInstance().beginTrans();
				if (finfo.getFieldName().equals("editImg")) {
					String edit = "UPDATE attachefile SET fileOrgName=?, fileSaveName=?, fileDir=?, fileSize=?, fileType=?, modified_date=? "
							+ "WHERE idAttacheFile=?";
					PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(edit);
					pstmt.setString(1, finfo.getFileOrgName());
					pstmt.setString(2, finfo.getFileSaveName());
					pstmt.setString(3, finfo.getFileDir());
					pstmt.setLong(4, finfo.getFileSize());
					pstmt.setString(5, finfo.getFileType());
					pstmt.setString(6, DateTime.getInstance().getNowDateTimeString());
					pstmt.setInt(7, image.getIdAttacheFile());
					pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
					finfo = null;
					File f = new File(image.getFileDir() + "/" + image.getFileSaveName());
					if (f.exists()) {
						f.delete();
					}
				}
				DBHandler.getInstance().commit();
			}
		} catch (Exception e) {
			DBHandler.getInstance().rollback();
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			AdminTableDAO.getInstance().updateEvent(new AdminTableCMD(idAdminBoard, eventType, eventLinkCode, boardTitle, boardText, brand, 
					start_date, end_date, finfo, isDisplay));
		} catch (Exception e) {
			DBHandler.getInstance().rollback();
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			AdminTableCMD cmd = AdminTableDAO.getInstance().getEvent(idAdminBoard);
			
			FileInfo image = cmd.getImage();
			if (image != null) {
				request.setAttribute("image", image);
			}
			request.setAttribute("eventType", CommonCodeDAO.getInstance().getCommonCodeList(null, "parentId=3"));
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, -1, "brandTitle", null));
			request.setAttribute("cmd", cmd);
			request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.');"
					+ "location.replace('updateEvent.do?id=" + cmd.getIdAdminBoard() + "');</script>");
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/updateEvent.jsp");
		dpr.forward(request, response);
	}

}